package com.bestcem.xm.user.grpc.client;

import com.alibaba.fastjson.JSON;
import com.bestcem.xm.common.core.constant.GrpcClientNames;
import com.bestcem.xm.common.core.uitls.GrpcDateUtil;
import com.bestcem.xm.qdes.grpc.v1.services.*;
import com.bestcem.xm.user.grpc.client.convert.UserBpProjectGrpcConvert;
import com.bestcem.xm.user.grpc.client.dto.ProjectDataDTO;
import com.zy.dp.proto.base.v1.Code;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * 问卷相关grpc服务
 *
 * @author wangyunyun <yunyun.wang@idiaoyan.com>
 * @version v1.0
 * @date 2022/6/9 14:00
 */
@Slf4j
@Service("userBpProjectGrpcService")
public class BpProjectGrpcService {

    @GrpcClient(GrpcClientNames.DS_QDES)
    private BpProjectServiceGrpc.BpProjectServiceBlockingStub stub;

    @Resource
    private UserBpProjectGrpcConvert userBpProjectGrpcConvert;

    /**
     * 执行问卷项目的copy_sample
     *
     * @param srcOrgId 源公司id
     * @param dstOrgId 目标公司id
     * @return Boolean
     * @author wangyunyun <yunyun.wang@idiaoyan.com>
     * @date 2022/6/9 14:08
     */
    public Boolean qdesCopySample(String srcOrgId, String dstOrgId) {
        // 设置请求参数
        QdesCopySampleRequest request = QdesCopySampleRequest.newBuilder().setDstId(dstOrgId).setSrcId(srcOrgId).build();
        // 请求
        QdesCopySampleResponse response = null;
        try {
            response = stub.qdesCopySample(request);
        } catch (Exception e) {
            log.error("执行问卷项目的copy_sample。请求失败", e);
        }
        // 返回值处理
        if (response != null && Code.OK_VALUE == response.getStatus().getCodeValue()) {
            return Boolean.TRUE;
        } else {
            log.error("执行问卷项目的copy_sample。请求失败, response:{}", response != null ? response.toString() : null);
        }
        return Boolean.FALSE;
    }

    /**
     * 通过创建时间批量查询租户的问卷项目数与答卷数
     *
     * @param orgIds    租户id列表
     * @param startTime 创建开始时间
     * @param endTime   创建结束时间
     * @return java.util.List<com.bestcem.xm.user.grpc.client.dto.ProjectDataDTO>
     * @author cuiqiongyu <qiongyu.cui@idiaoyan.cn>
     * @date 2022/06/14 20:25
     */
    public List<ProjectDataDTO> listProjectCountsByCreateTime(List<String> orgIds, Date startTime, Date endTime) {
        if (log.isInfoEnabled()) {
            log.info("通过创建时间批量查询租户的问卷项目数与答卷数：orgIds:{}, startTime:{}, endTime:{}",
                    JSON.toJSONString(orgIds), startTime, endTime);
        }

        ListProjectCountsByCreateTimeRequest.Builder builder = ListProjectCountsByCreateTimeRequest.newBuilder();
        builder.addAllOrgIds(orgIds);
        builder.setStartTime(GrpcDateUtil.getTimestamp(startTime));
        builder.setEndTime(GrpcDateUtil.getTimestamp(endTime));
        ListProjectCountsByCreateTimeRequest build = builder.build();

        // 请求问卷项目
        ListProjectCountsByCreateTimeResponse response = stub.listProjectCountsByCreateTime(build);
        // 校验查询结果
        if (Code.ERROR_VALUE == response.getStatus().getCodeValue()) {
            log.error("通过创建时间批量查询租户的问卷项目数与答卷数：dto:{} orgIds:{}, startTime:{}, endTime:{}", response.getStatus().getMessage(),
                    JSON.toJSONString(orgIds), startTime, endTime);
            return Collections.emptyList();
        }

        return userBpProjectGrpcConvert.responseToProjectDatas(response.getProjectsList());
    }

    /**
     * 通过全局配置关闭租户下所有问卷IP配置
     *
     * @param orgId 租户id
     * @return java.lang.Boolean
     * @author panyun <yun.pan@idiaoyan.com>
     * @date 2022/7/11 15:45
     */
    //public Boolean closeProjectIpAttr(String orgId) {
    //    if (log.isInfoEnabled()) {
    //        log.info("通过全局配置关闭租户下所有问卷IP配置：orgId:{}", orgId);
    //    }
    //
    //    UpdateProjectIPAttrRequest.Builder builder = UpdateProjectIPAttrRequest.newBuilder();
    //    builder.setOrgId(orgId);
    //
    //    UpdateProjectIPAttrResponse response = stub.updateProjectIPAttr(builder.build());
    //
    //    // 校验查询结果
    //    if (Code.ERROR_VALUE == response.getStatus().getCodeValue()) {
    //        log.error("通过全局配置关闭租户下所有问卷IP配置失败：orgId:{}", orgId);
    //        return Boolean.FALSE;
    //    }
    //    return Boolean.TRUE;
    //}

}
